<?php





namespace app\admin\model;

use think\facade\Db;





use think\Model;



class BalanceLogModel extends Model

{



    // 设置字段信息

    protected $schema = [



    ];

    /**

     * 类型转换

     * @var string[]

     */

    protected $type = [



    ];

    /**

     * @var string

     */

    protected $name = 'customer_balance_log';



    protected $defaultSoftDelete = 0;



    protected $autoWriteTimestamp = true;

    /**

     * 驼峰格式转换

     * @var bool

     */

    protected $convertNameToCamel = true;



    public function shop()

    {

        return $this->hasOne(ShopModel::class, 'shop_id', 'shop_id')->field('shop_id,shop_name');

    }



    public function customer()

    {

        return $this->hasOne(CustomerModel::class, 'id', 'customer_id')->field('id,customer_name');

    }



    /**

     * @param $value

     * @return false|string

     * @author ran

     * @date 2021-12-13 18:00

     * mailbox 466180170@qq.com

     */

    public function getcreateTimeAttr($value)

    {

        return date('Y-m-d', $value);

    }



    /**

     * 欠款待计算

     * @return string

     * @author ran

     * @date 2021-12-13 18:40

     * mailbox 466180170@qq.com

     */

    public function getdebtMoneyAttr()

    {

        return "0.00";

    }

    protected $append = ['debtMoney'];

    /**

     * 资金明细

     * @author ran

     * @date 2021-12-10 19:30

     * mailbox 466180170@qq.com

     */

    public function getAdminShopCapitalStatisticsabk(int $page, int $limit,$request)

    {

        $where = [];

        $field = 'b.shop_id,b.customer_id,cash,pay_by_car,wechat_or_alipay,drawer_id,b.create_time,

        by_stages,store_transfer,platform_collection,c.customer_name,s.shop_name,sp.name';

        $shopId = isset($request['shopId']) && intval($request['shopId']) ? intval($request['shopId']) : 0;

        $shopId && array_push($where, ['b.shop_id', '=', $shopId]);

        $connect = $this->alias('b')

            ->where($where)

            ->join('customer c','b.customer_id = c.id')

            ->join('shop s','b.shop_id = s.shop_id')

            ->leftjoin('shop_person sp','b.drawer_id = sp.id')

            ->field($field)

            ->order('b.create_time', 'desc');

        return empty($connect) ? ['dataCount' => $connect->count(), 'list' => []] : ['dataCount' => $connect->count(), 'list' => $connect->page($page, $limit)->select()->toArray()];

    }

    /**

     * 资金日报表

     * @author ran

     * @date 2021-12-10 19:30

     * mailbox 466180170@qq.com

     */

    public function getAdminShopCapitalStatistics($shopIds,int $page, int $limit,$request)

    {

        $startTime = strtotime($request['start_time'] ?? 0);

        $endTime = strtotime($request['end_time'] ?? 0);

        $shop_person_id = $request['shop_person_id'] ?? 0;

       $sysModel = new SysModel();
       $position_id = $sysModel->getPersonPositionId($shop_person_id);

       $person_where = [];
       if(!empty($position_id)){
           array_push($person_where,['sale_id','=',$shop_person_id]);
       }

        $filed ='id,consignee as customerName,deal_time as createTime,sale_name as name';

        $connect =Db::name('shop_refund_order')->field($filed)

            ->where('status','<>',3)

            ->where('refund_type','=','101.02')

            ->where($person_where)

            ->when($shopIds ?? [], function ($query) use ($shopIds) {

                return $query->whereIn('shop_id', $shopIds);

            })

            ->when($startTime ?? 0, function ($query) use ($startTime) {

                return $query->whereTime('deal_time', '>=', $startTime);

            })

            ->when($endTime ?? 0, function ($query) use ($endTime) {

                return $query->whereTime('deal_time', '<=', $endTime);

            },function ($query) use ($endTime) {

                return $query->whereMonth('deal_time');

            })->order('create_time desc');

        $count =$connect-> count();


        $allOrderIds = $connect->column('id');
        $list =$connect->page($page, $limit)->select();

        $list =empty($list) ? [] : $list->toArray();

        $orderIds =array_column($list,'id');

        $field='sum(pay_price) as money,order_id';

        //现金

        $cash =Db::name('shop_refund_order_goods')

            ->field($field)

            ->whereIn('order_id',$orderIds)

            ->where('refund_mode','01')->group('order_id')->select()->toArray();

        $cash=array_column($cash,null,'order_id');

        $sumCash = Db::name('shop_refund_order_goods')

            ->whereIn('order_id',$allOrderIds)

            ->where('refund_mode','01')

            ->sum('pay_price');

        //刷卡/微信/支付宝

        $wechatOrAlipay =Db::name('shop_refund_order_goods')

            ->field('sum(pay_price) as money,order_id')

            ->whereIn('order_id',$orderIds)

            ->where('refund_mode','02')->group('order_id')->select()->toArray();

        $wechatOrAlipay=array_column($wechatOrAlipay,null,'order_id');

        $allWechatOrAlipay = Db::name('shop_refund_order_goods')

            ->whereIn('order_id',$allOrderIds)

            ->where('refund_mode','02')

            ->sum('pay_price');

        //分期

        $byStages =Db::name('shop_refund_order_goods')

            ->field($field)

            ->whereIn('order_id',$orderIds)

            ->where('refund_mode','04')->group('order_id')->select()->toArray();

        $byStages=array_column($byStages,null,'order_id');

        $allByStages = Db::name('shop_refund_order_goods')

            ->whereIn('order_id',$allOrderIds)

            ->where('refund_mode','04')

            ->sum('pay_price');

        //平台收款

        $platformCollection =Db::name('shop_refund_order_goods')

            ->field($field)

            ->whereIn('order_id',$orderIds)

            ->where('refund_mode','05')->group('order_id')->select()->toArray();

        $platformCollection=array_column($platformCollection,null,'order_id');

        $allPlatformCollection = Db::name('shop_refund_order_goods')

            ->whereIn('order_id',$allOrderIds)

            ->where('refund_mode','05')

            ->sum('pay_price');


        //店转

        $storeTransfer =Db::name('shop_refund_order_goods')

            ->field($field)

            ->whereIn('order_id',$orderIds)

            ->where('refund_mode','06')->group('order_id')->select()->toArray();

        $storeTransfer=array_column($storeTransfer,null,'order_id');

        $allStoreTransfer = Db::name('shop_refund_order_goods')

            ->whereIn('order_id',$allOrderIds)

            ->where('refund_mode','06')

            ->sum('pay_price');




        foreach($list as $key=>$value){

            $list[$key]['debtMoney']=0;

            $list[$key]['storeTransfer']=$storeTransfer[$value['id']]['money']??0;

            $list[$key]['platformCollection']=$platformCollection[$value['id']]['money']??0;

            $list[$key]['byStages']=$byStages[$value['id']]['money']??0;

            $list[$key]['payByCar']=0;

            $list[$key]['wechatOrAlipay']=$wechatOrAlipay[$value['id']]['money']??0;

            $list[$key]['cash']=$cash[$value['id']]['money']??0;

            $list[$key]['createTime']=date('Y-m-d',$value['createTime']);

        }
        $sum = [
            'id'=>"",
            'customerName'=>"合计",
            'name'=>"",
            'debtMoney'=>0,
            'storeTransfer'=>$allStoreTransfer??0,
            'platformCollection'=>$allPlatformCollection??0,
            'byStages'=>$allByStages??0,
            'payByCar'=>0,
            'wechatOrAlipay'=>$allWechatOrAlipay??0,
            'cash'=>$sumCash??0,
            'createTime'=>""
        ];
        array_push($list,$sum);

        return [

            'list' =>  $list,

            'dataCount' => $count

        ];

    }

    /**

     * @author ran

     * @date 2021-12-13 19:06

     * mailbox 466180170@qq.com

     */

    public function getConsumeBalanceLogByShopId($shopId)

    {

        return $this->where('mode',2)->where('shop_id',$shopId)->sum('mode_money');

    }



}
